<template>
    <input-select :id="id" :styles="styles" :options="options" isChange="Y" :index="index"   :data="data"  @input="changeData" ></input-select>
</template>
<script>
import InputSelect  from '../input/input-select'
export default {
    props:['id','styles','value','data','index','appid','exkey'],
    name: "lt-select",
    components: {
        InputSelect
    },
     data(){
        return{
            options:[],
            oldTc:false,
            isNotDel:"N"
        }
    },
    created(){
        this.$post("bygex","/byg/funeral/getLtSelect",{type:'告别厅'}).then(data=>{
            this.options=[];
            let item={optname:"",optvalue:"",sf_ex24:"是"};
            this.options.push(item)
            for(let i=1;i<data.length;i++){
                this.options.push(data[i])
            }
        })
        /*try{
            if(this.exkey&&this.exkey.isNotDel){
                this.isNotDel=this.exkey.isNotDel
            }
        }catch(e){}*/
    },
    watch:{
        data:{
            handler: function (newVal) {
                console.log("watch===================="+newVal)
                let isHas=false;
                let ltId="";
                let ltName="";
                let istc=false;
                this.data["item"].forEach(item => {
                    let szi_ex7=item.szi_ex7;
                    let szi_ex16=item.szi_ex16;
                    if(szi_ex7=="告别厅"||szi_ex16=="告别厅"){
                        isHas=true;
                        ltName=item.szi_ex1;
                        ltId=item.szi_ex8;

                        if(szi_ex16=="告别厅")
                            istc=true;
                    }
                });
                let ids=this.id.split(".")
                let lt_ex4="";
                let lt_ex3="";
                if(this.index==0||this.index=="0"||this.index){
                    lt_ex4=this.data[ids[0]][this.index]["lt_ex4"]
                    lt_ex3=this.data[ids[0]][this.index]["lt_ex3"]
                }else{
                    lt_ex4=this.data[ids[0]]["lt_ex4"]
                    lt_ex3=this.data[ids[0]]["lt_ex3"]
                }
               // console.log(JSON.stringify(this.data),"data===sss=s=s=s====")
                console.log("ltName=="+isHas+"=="+ltName+"=="+ltId+"=="+this.index+"=="+this.id+":lt_ex4="+lt_ex4)
                
                if(!isHas&&!lt_ex3){
                    
                    if(this.index==0||this.index=="0"||this.index){
                       
                        this.$set(this.data[ids[0]][this.index],"lt_ex3","");
                        this.$set(this.data[ids[0]][this.index],"lt_ex4","");
                        //console.log("ids[0]=="+ids[0]+"="+this.index+"="+this.data[ids[0]][this.index]["lt_ex3"])
                       // console.log(this.data)
                    }else{
                       
                        this.$set(this.data[ids[0]],"lt_ex3","");
                        this.$set(this.data[ids[0]],"lt_ex4","");
                    }
                }else if(!istc){
                    if(this.index==0||this.index=="0"||this.index){
                        if(this.data[ids[0]][this.index]["lt_ex4"]!=ltId){
                            this.$set(this.data[ids[0]][this.index],"lt_ex3",ltName);
                            this.$set(this.data[ids[0]][this.index],"lt_ex4",ltId);
                        }
                    }else{
                        if(this.data[ids[0]]["lt_ex4"]!=ltId){
                            this.$set(this.data[ids[0]],"lt_ex3",ltName);
                            this.$set(this.data[ids[0]],"lt_ex4",ltId);
                        }
                    }
                   
                }
            },
            deep: true    //深度监听
        }
    },
    methods:{
        changeData(data){
            
            let datas=JSON.parse(data);
            datas.addType="string";
            datas.itemType='告别厅';
            datas.nameType="告别厅";
            let vals=datas.val;
            let isTc=false;
            let id="";

            for(let item_ of this.options){
                console.log("options============")
                console.log(item_)
                if(item_.optname==vals&&vals){
                    if(item_.sf_ex24=="是"){
                        isTc=true;
                        id=item_.sf_id;
                        this.oldTc=true;
                    }
                }
            }
            if(!vals){
                isTc=this.oldTc;
            }
            this.$emit("input",data);
           // console.log(this.options)
            console.log("isTc==========================================="+isTc)
            if(!isTc){
                
                this.$emit("funeralItemSelectEvent",JSON.stringify(datas))
            }else{
                this.$post("bygex","/byg/funeral/getFuneralSfMealItem",{sId:id}).then(rows=>{
                    let isOk=true;
                    let delItem=[];
                
                    for(let i=0;i<this.data["item"].length;i++){
                        if(vals!=""&&this.data["item"][i].szi_ex15.indexOf(vals)!=-1){
                            isOk=false;
                        }
                        if(this.data["item"][i].szi_ex16=="告别厅"&&(this.data["item"][i].szi_ex15.indexOf(vals)==-1||vals=='')){
                            delItem.push(this.data["item"][i].szi_ex8)
                        }
                        
                    }
                    console.log("isOk===="+isOk+"=="+vals);
                    console.log(delItem)

                    if(isOk){
                        if(delItem.length>0){
                            
                            for(let id of delItem){
                                let index=-1;
                                for(let i=0;i<this.data["item"].length;i++){

                                    if(this.data["item"][i].szi_ex8==id){
                                        index=i;
                                        break;
                                    }
                                }
                                if(index>=0){
                                    this.$delete(this.data["item"],index)
                                }
                            }
                            
                        }
                      
                        if(rows.length>0){
                            for(let row_ of rows){
                                row_.szi_ex13="业务";
                                row_.szi_ex15=vals;
                               
                                this.$set(this.data["item"],this.data["item"].length,row_);
                            }
                        }
                        
                        

                    }
                });
            }
            
        }
    }
}
</script>